<script lang="ts" setup>
import useStore from '@/stores';
import { reactive, computed, nextTick } from 'vue';
import { traduction } from '@/utils/language';
import { showElMessage } from '@/utils/utils';
import { getNetWorkData } from './network.server';
import { initNetworkData } from './networkFunctions/networkDataFactory';
import { UserPrivil } from '@/model/base-enum';
import { checkPrivil } from '@/apps/app-bmc/router/routes';

import Dns from './components/Dns.vue';
import Vlan from './components/Vlan.vue';
import Lldp from './components/Lldp.vue';
import Hostname from './components/Hostname.vue';
import IpInput from '@/components/IpInput/IpInput.vue';
import NetworkPortMode from './components/NetworkPortMode.vue';
import NetworkProtocols from './components/NetworkProtocols.vue';
import GlobalStoreService from '@/services/gloabal-store-service';

const globalStoreService = new GlobalStoreService();
const errorMsg = traduction('COMMON_FAILED');

// 权限
const permission = checkPrivil(UserPrivil.baseConfig);
const store = useStore();
const isSystemLock = computed(() => {
  return store.state.glob.systemLocked;
});

const networkPort: any = reactive({});
const networkProtocol: any = reactive({});
const networkDns: any = reactive({});
const networkVlan: any = reactive({});
const dedicatedVlan: any = reactive({});
const networkLldp: any = reactive({});

// 设置网口模式、网络协议、DNS、VLAN、LLDP的label的宽度相同，取其中的最大值
function initLabelWidth() {
  const list: any = document.querySelectorAll('.network-page-label');
  const resDomList: any = [];
  let resWidth = 0;
  list.forEach((item: any) => {
    if (
      item.classList.contains('el-form-item') &&
      item.children &&
      item.children[0] &&
      item.children[0].classList.contains('el-form-item__label-wrap')
    ) {
      resWidth = Math.max(item.children[0].clientWidth, resWidth);
      resDomList.push(item.children[0]);
    } else {
      resWidth = Math.max(item.clientWidth, resWidth);
      resDomList.push(item);
    }
  });

  resDomList.forEach((item: any) => {
    item.style.width = resWidth + 'px';
  });
}

// 初始化展示数据
function initShowData(data: any) {
  const showData = initNetworkData(data);
  Object.assign(networkPort, showData.networkPort);
  Object.assign(networkProtocol, showData.networkProtocol);
  Object.assign(networkDns, showData.networkDns);
  Object.assign(networkVlan, showData.networkVlan);
  Object.assign(dedicatedVlan, showData.dedicatedVlan);
  Object.assign(networkLldp, showData.networkLldp);
}

// 获取数据
function getData() {
  globalStoreService.setValue('isLoading', true);
  getNetWorkData()
    .then((data: any) => {
      if (data) {
        initShowData(data);
      }
      nextTick(() => {
        initLabelWidth();
      });
      globalStoreService.setValue('isLoading', false);
    })
    .catch(() => {
      globalStoreService.setValue('isLoading', false);
    });
}

// 修改主机名成功
function setHostName(value: string) {
  networkPort.hostName = value;
}
// 获取数据
getData();
</script>

<template>
  <div id="networkViewId" class="network-box">
    <!-- 主要内容区域 -->
    <div class="content-details">
      <!-- 主机名 -->
      <Hostname
        :data="networkPort"
        :permission="permission"
        :lock="isSystemLock"
        @set-host-name="setHostName"
      />

      <!-- 网口模式 -->
      <NetworkPortMode
        v-if="networkPort.productType"
        :permission="permission"
        :lock="isSystemLock"
        :data="networkPort"
        @init-show-data="initShowData"
      />

      <!-- 网络协议 -->
      <NetworkProtocols
        :data="networkProtocol"
        :permission="permission"
        :lock="isSystemLock"
        @init-show-data="initShowData"
      />

      <!-- DNS -->
      <Dns
        :data="networkDns"
        :permission="permission"
        :lock="isSystemLock"
        @init-show-data="initShowData"
      />

      <!-- Ncsi VLAN -->
      <Vlan
        v-if="networkVlan.productTypeVlan"
        :permission="permission"
        :lock="isSystemLock"
        :data="networkVlan"
        :is-n-c-s-i="true"
        @init-show-data="initShowData"
      />

      <!-- Dedicated VLAN -->
      <Vlan
        v-if="dedicatedVlan.productTypeVlan"
        :permission="permission"
        :lock="isSystemLock"
        :data="dedicatedVlan"
        :is-n-c-s-i="false"
        @init-show-data="initShowData"
      />

      <!-- LLDP -->
      <Lldp
        v-if="networkLldp.lldpSupport"
        :permission="permission"
        :lock="isSystemLock"
        :data="networkLldp"
        @init-show-data="initShowData"
      />
    </div>
  </div>
</template>

<style lang="scss" scoped>
.network-box {
  padding-right: 24px;

  :deep(.el-radio.el-radio--large .el-radio__label) {
    font-size: 12px;
  }

  :deep(.el-popper) {
    max-width: 250px !important;
    min-width: auto !important;
    width: auto !important;
  }

  :deep(.el-form-item) {
    .el-form-item__label {
      position: relative;

      &::before {
        position: absolute;
        left: -6px;
      }
    }
  }

  :deep(.network-page-label) {
    margin-right: 0 !important;

    .el-form-item__label-wrap {
      margin-right: 0 !important;
    }
  }
}
</style>
